
<div class="col-md-6 col-lg-4">
    <div class="card subject-card {{#if subject.alerts.length}}border-danger{{else if subject.warnings.length}}border-warning{{else}}border-primary{{/if}}">
        {{#if subject.alerts.length }}
        <div class="card-header card-header-warning d-flex justify-content-between align-items-center">
            <strong>{{subject.group_no}} ({{subject.user_id}}-{{subject.id}})</strong>
            <span class="badge bg-danger">紧急</span>
        </div>
        {{else if subject.warnings.length}}
        <div class="card-header card-header-alert d-flex justify-content-between align-items-center">
            <strong>{{subject.group_no}} ({{subject.user_id}}-{{subject.id}})</strong>
            <span class="badge bg-warning">警告</span>
            <span> {{subject.alerts.length}} {{subject.warnings.length}} {{subject.infos.length}}</span>
        </div>
        {{else}}
        <div class="card-header d-flex justify-content-between align-items-center">
            <strong>{{subject.group_no}} ({{subject.user_id}}-{{subject.id}})</strong>
        </div>
        {{/if}}
        <div class="card-body">

            <div class="timeline">
                {{# if (lt subject.status 3) }}
                <div class="timeline-item">
                    <span class="badge bg-info">关注</span>
                    <span class="timeline-date"> {{subject.created_date}}</span>
                </div>
                <div class="timeline-item">
                    <span class="badge bg-secondary">报名</span>
                    <span class="timeline-date"> {{subject.apply_date}}</span>
                </div>
                {{else if (lt subject.status 5) }}
                <div class="timeline-item">
                    <span class="badge bg-info">入组</span>
                    <span class="timeline-date"> {{subject.entry_date}}</span>
                </div>
                <div class="timeline-item">
                    <span class="badge bg-secondary">分组</span>
                    <span class="timeline-date"> {{subject.group_date}}</span>
                </div>
                <div class="timeline-item">
                    <span class="badge bg-primary">开始</span>
                    <span class="timeline-date"> {{subject.start_date}}</span>
                </div>
                {{else if (lt subject.status 9) }}
                <div class="timeline-item">
                    <span class="badge bg-info">出组</span>
                    <span class="timeline-date"> {{subject.exit_date}}</span>
                </div>
                {{else}}
                <div class="timeline-item">
                    <span class="badge bg-info">结束</span>
                    <span class="timeline-date"> {{subject.finish_date}}</span>
                </div>
                {{/if}}
            </div>

            <!-- 警报、警告和提示信息 -->
            <div class="alert-list">
            {{#if subject.dayStats.alerts.length}}
                {{#each subject.dayStats.alerts as |alert|}}
                <div class="alert-item"><i class="fas fa-exclamation-triangle"></i> {{alert}} </div>
                {{/each}}
            {{/if}}
            {{#if subject.diaryStats.alerts.length}}
                {{#each subject.diaryStats.alerts as |alert|}}
                <div class="alert-item"><i class="fas fa-exclamation-triangle"></i> {{alert}} </div>
                {{/each}}
            {{/if}}
            {{#if subject.taskStats.alerts.length}}
                {{#each subject.taskStats.alerts as |alert|}}
                <div class="alert-item"><i class="fas fa-exclamation-triangle"></i> {{alert}} </div>
                {{/each}}
            {{/if}}
            </div>
            <div class="warning-list">
            {{#if subject.dayStats.warnings.length}}
                {{#each subject.dayStats.warnings as |warning|}}
                <div class="warning-item"><i class="fas fa-exclamation-circle"></i> {{warning}} </div>
                {{/each}}
            {{/if}}
            {{#if subject.diaryStats.warnings.length}}
                {{#each subject.diaryStats.warnings as |warning|}}
                <div class="warning-item"><i class="fas fa-exclamation-circle"></i> {{warning}} </div>
                {{/each}}
            {{/if}}
            {{#if subject.taskStats.warnings.length}}
                {{#each subject.taskStats.warnings as |warning|}}
                <div class="warning-item"><i class="fas fa-exclamation-circle"></i> {{warning}} </div>
                {{/each}}
            {{/if}}
            </div>
            <div class="tip-list">
            {{#if subject.dayStats.tips.length}}
                {{#each subject.dayStats.tips as |tip|}}
                <div class="tip-item"><i class="fas fa-lightbulb"></i> {{tip}} </div>
                {{/each}}
            {{/if}}
            {{#if subject.diaryStats.tips.length}}
                {{#each subject.diaryStats.tips as |tip|}}
                <div class="tip-item"><i class="fas fa-lightbulb"></i> {{tip}} </div>
                {{/each}}
            {{/if}}
            {{#if subject.taskStats.tips.length}}
                {{#each subject.taskStats.tips as |tip|}}
                <div class="tip-item"><i class="fas fa-lightbulb"></i> {{tip}} </div>
                {{/each}}
            {{/if}}
            </div>

            <div class="card-meta mt-3">
            {{#if subject.start_time}}
                <div class="d-flex justify-content-between">
                    <span><i class="fa fa-rocket"></i> 试验开始</span>
                    <span>进度: {{subject.dayStats.progress}}%</span>
                    <span class="badge bg-info">{{subject.dayStats.days_since_start}}/{{subject.groupConfig.duration}}天</span>
                </div>
                <div class="progress mt-1" style="height: 8px;">
                    <div class="progress-bar
                        {{#if subject.dayStats.alerts.length}}
                            progress-bar-danger
                         {{else if subject.dayStats.warnings.length }}
                            progress-bar-warning
                         {{else}}
                            progress-bar-success
                         {{/if}}"
                        role="progressbar"
                        style="width: {{subject.dayStats.progress}}%;"
                        aria-valuenow="{{subject.dayStats.progress}}"
                        aria-valuemin="0"
                        aria-valuemax="100">
                    </div>
                </div>
                <div class="progress-stats">
                    <span>{{subject.dayStats.start_date}}</span>
                    <span>还剩：{{subject.dayStats.day_remaining}}天</span>
                </div>
            {{/if}}
            </div>

            {{#if (gte subject.status '3') }}

            <!-- 日记打卡进度 -->
            <div class="card-meta mt-3">
                <div class="d-flex justify-content-between align-items-center">
                    <span><i class="fas fa-calendar-check"></i> 日记打卡</span>
                    <span>进度: {{subject.diaryStats.progress}}% 完成</span>
                    <span class="badge bg-info">{{subject.diaryStats.completed}}/{{subject.diaryStats.total}}天</span>
                </div>
                <div class="progress" style="height: 8px;">
                    <div class="progress-bar
                         {{#if subject.diaryStats.alerts.length}}
                            progress-bar-danger
                         {{else if subject.diaryStats.warnings.length}}
                            progress-bar-warning
                         {{else}}
                            progress-bar-success
                         {{/if}}"
                        role="progressbar"
                        style="width:{{subject.diaryStats.progress}}%;"
                        aria-valuenow="width:{{subject.diaryStats.progress}}%;"
                        aria-valuemin="0"
                        aria-valuemax="100">
                    </div>
                </div>
                <div class="progress-stats">
                    <span>累计打卡: {{subject.diaryStats.completed}}次</span>
                    {{#if subject.diaryStats.days_since_last_diary}}
                    <span class="text-warning">{{subject.diaryStats.days_since_last_diary}}天未打卡</span>
                    {{else}}
                    <span class="text-success">今日无需打卡</span>
                    {{/if}}
                </div>
            </div>

            <!-- 随访任务进度 -->
            <div class="card-meta mt-3">
                <div class="d-flex justify-content-between align-items-center">
                    <span><i class="fas fa-tasks"></i> 随访任务</span>
                    <span>进度：{{subject.taskStats.progress}}%</span>
                    <span class="badge bg-info">{{subject.taskStats.completed}}/{{subject.taskStats.total}}个</span>
                </div>
                <div class="progress" style="height: 8px;">
                    <div class="progress-bar 
                         {{#if subject.taskStats.alerts.length}}
                            progress-bar-danger
                         {{else if subject.taskStats.warnings.length}}
                            progress-bar-warning
                         {{else}}
                            progress-bar-success
                         {{/if}}"
                        role="progressbar"
                        style="width:{{subject.taskStats.progress}}%;"
                        aria-valuenow="width:{{subject.taskStats.progress}}%;"
                        aria-valuemin="0"
                        aria-valuemax="100">
                    </div>
                </div>
                <div class="progress-stats">
                    <span>已完成: {{subject.taskStats.completed}}个</span>
                    {{#if subject.taskStats.overdue}}
                    <span class="text-danger">逾期: {{subject.taskStats.overdue}}个</span>
                    {{/if}}
                    <span class="text-warning">待完成: {{subject.taskStats.pending}}个</span>
                </div>
            </div>

            {{/if}}

            <div class="latest-update mt-3">
                <div class="latest-update-title">
                    最新动态
                </div>
                <div class="latest-update-content">
                    {{#if subject.latestSupplyInfo.status}}
                    <div>

                        {{#if subject.latestSupplyInfo.status.timestamp}}
                        <div>
                            <span>
                                <i class="fas fa-truck"></i>
                                {{formatDate subject.latestSupplyInfo.status.timestamp "YYYY-MM-DD"}}
                            </span>
                            <span class="badge {{#if (eq subject.latestSupplyInfo.status.code 'delivered')}}bg-success{{else if (eq subject.latestSupplyInfo.status.code 'shipped')}}bg-info{{else}}bg-secondary{{/if}}">
                                {{#if (eq subject.latestSupplyInfo.status.code 'delivered')}}已送达{{else if (eq subject.latestSupplyInfo.status.code 'shipped')}}已发货{{else if (eq subject.latestSupplyInfo.status.code 'preparing')}}准备中{{else}}未发货{{/if}}
                            </span>
                            <span>

                                 {{subject.latestSupplyInfo.item_info}}
                                 {{subject.latestSupplyInfo.express_id}}
                            </span>
                        </div>
                        <div>
                            <span>
                                {{subject.latestSupplyInfo.receiver.name}}
                                {{subject.latestSupplyInfo.receiver.address}}
                                {{subject.latestSupplyInfo.receiver.phone}}
                            </span>
                        </div>
                        {{/if}}

                    </div>
                    {{/if}}

                    {{#if subject.latestProfile.content}}
                    <div>
                        <span>
                            <i class="fas fa-comment-dots"></i> {{formatDate subject.latestProfile.created_time 'YYYY-MM-DD HH:mm:ss'}}
                        </span>
                        <span>
                            {{subject.latestProfile.content}}
                        </span>
                    </div>
                    {{/if}}

                    {{#if subject.exit_reason}}
                    <div>
                        <span>
                            <i class="fas fa-stop-circle"></i> {{subject.daysStats.exit_date}}
                        </span>
                        <span>
                            退出原因：{{subject.exit_reason}}
                        </span>
                    </div>

                    {{/if}}

                </div>
            </div>

            <div class="text-end mt-3">
                <a href="/apps/trial/admin/trials/{{subject.trial_id}}/subjects/{{subject.id}}" class="btn btn-sm btn-primary">
                    <i class="fas fa-eye"></i> 查看详情
                </a>
                <!--
                <button type="button" class="btn btn-sm btn-secondary"
                    data-bs-toggle="modal"
                    data-bs-target="#profileModal">
                    <i class="fas fa-comment me-1"></i>备注档案
                </button>
                -->
            </div>

        </div>
    </div>
</div>
